﻿Imports System.Data.OleDb
''' <summary>
''' 表S_App数据访问类
''' </summary>
Public Class ClsAppDbAccess
    Inherits ClsBaseDbAccess

    'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\自写系统\rjcwxt\Rjcwxt\data\sys.mdb;Persist Security Info=True

    'Private mConnectionString As String '数据库连接字符串
    'Private mAppTableName As String 'S_App在库中的表名 S_App

    Public Sub New(ByVal s As String)
        MyBase.New(s, TABLE_NAME_APP)
    End Sub

    Public Sub New(ByVal s As String, ByVal n As String)
        MyBase.New(s, n)
    End Sub


    ''' <summary>
    ''' 返回S_App的程序信息
    ''' </summary>
    ''' <returns>类clsApp对象</returns>
    Public Function GetAppInfo() As ClsApp

        Try

            Dim tmpClsApp As ClsApp
            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection()

            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "select * from [" & TABLE_NAME_APP & "] "
            '将一行数据转为类对象
            tmpClsApp = CType(ConvertRecordToClass(tmpDbAccess.ExecuteSqlReturnDataSet(tmpSql)), ClsApp)
            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpClsApp

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "clsAppDbAccess - GetAppInfo"

            Return Nothing
        End Try

    End Function

    ''' <summary>
    ''' 更新App程序信息内容,
    ''' </summary>
    ''' <param name="tmpClsApp">clsApp类对象</param>
    ''' <returns>非0值成功</returns>
    Public Function UpdateAppInfo(ByVal tmpClsApp As ClsApp) As Integer
        'AppName,AppVerSion,AppCompany,AppWebUrl,AppEmail,AppMemo,AppPubTime,
        'AppType, AppExpire, AppRegisted, AppRegCode

        Try

            'Dim tmpClsApp As clsApp
            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection()
            Dim tmpParas(11) As OleDb.OleDbParameter 'Collection '= New OleDb.OleDbParameterCollection()
            Dim tmpReturn As Integer

            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "update [" & TABLE_NAME_APP & "] set [AppName]=@AppName, [AppVersion]=@AppVersion, [AppCompany]=@AppCompany,"
            tmpSql &= "[AppWebUrl]=@AppWebUrl, [AppEmail]=@AppEmail, [AppMemo]=@AppMemo, [AppPubTime]=@AppPubTime,"
            tmpSql &= "[AppType]=@AppType, [AppExpire]=@AppExpire, [AppRegisted]=@AppRegisted, [AppRegCode]=@AppRegCode"
            tmpSql &= " where [AppId]=@AppId"
            '构造参数集合
            tmpParas(0) = New OleDb.OleDbParameter("@AppName", OleDb.OleDbType.VarWChar)
            tmpParas(1) = New OleDb.OleDbParameter("@AppVersion", OleDb.OleDbType.VarWChar)
            tmpParas(2) = New OleDb.OleDbParameter("@AppCompany", OleDb.OleDbType.VarWChar)
            tmpParas(3) = New OleDb.OleDbParameter("@AppWebUrl", OleDb.OleDbType.VarWChar)
            tmpParas(4) = New OleDb.OleDbParameter("@AppEmail", OleDb.OleDbType.VarWChar)
            tmpParas(5) = New OleDb.OleDbParameter("@AppMemo", OleDb.OleDbType.VarWChar)
            tmpParas(6) = New OleDb.OleDbParameter("@AppPubTime", OleDb.OleDbType.DBTimeStamp)
            tmpParas(7) = New OleDb.OleDbParameter("@AppType", OleDb.OleDbType.VarWChar)
            tmpParas(8) = New OleDb.OleDbParameter("@AppExpire", OleDb.OleDbType.DBTimeStamp)
            tmpParas(9) = New OleDb.OleDbParameter("@AppRegisted", OleDb.OleDbType.Boolean)
            tmpParas(10) = New OleDb.OleDbParameter("@AppRegCode", OleDb.OleDbType.VarWChar)
            tmpParas(11) = New OleDb.OleDbParameter("@AppId", OleDb.OleDbType.Integer)

            tmpParas(0).Value = tmpClsApp.AppName
            tmpParas(1).Value = tmpClsApp.AppVerSion
            tmpParas(2).Value = tmpClsApp.AppCompany
            tmpParas(3).Value = tmpClsApp.AppWebUrl
            tmpParas(4).Value = tmpClsApp.AppEmail
            tmpParas(5).Value = tmpClsApp.AppMemo
            tmpParas(6).Value = tmpClsApp.AppPubTime
            tmpParas(7).Value = tmpClsApp.AppType
            tmpParas(8).Value = tmpClsApp.AppExpire
            tmpParas(9).Value = tmpClsApp.AppRegisted
            tmpParas(10).Value = tmpClsApp.AppRegCode
            tmpParas(11).Value = tmpClsApp.AppId


            tmpReturn = tmpDbAccess.UpdateRecord(tmpSql, tmpParas)

            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpReturn

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "clsAppDbAccess - GetAppInfo"

            Return 0
        End Try

    End Function

    ''' <summary>
    ''' 将数据库记录转换为ClsApp类对象
    ''' </summary>
    ''' <param name="tmpTableReader">OleDbTableReader</param>
    ''' <returns>失败或没有数据则返回Nothing</returns>
    Public Overrides Function ConvertRecordToClass(ByVal tmpTableReader As OleDb.OleDbDataReader) As Object
        'AppName,AppVerSion,AppCompany,AppWebUrl,AppEmail,AppMemo,AppPubTime,
        'AppType, AppExpire, AppRegisted, AppRegCode
        If tmpTableReader Is Nothing Then
            Return Nothing
        End If

        Dim tmpCls As ClsApp = Nothing '= New clsApp()

        Try

            If tmpTableReader.Read() Then
                tmpCls = New ClsApp()

                With tmpCls

                    .AppId = tmpTableReader.GetInt32(0)
                    .AppName = tmpTableReader.GetString(1)
                    .AppVerSion = tmpTableReader.GetString(2)
                    .AppCompany = tmpTableReader.GetString(3)
                    .AppWebUrl = tmpTableReader.GetString(4)
                    .AppEmail = tmpTableReader.GetString(5)
                    .AppMemo = tmpTableReader.GetString(6)
                    .AppPubTime = tmpTableReader.GetDateTime(7)  '.GetString(7)
                    .AppType = tmpTableReader.GetString(8)
                    .AppExpire = tmpTableReader.GetDateTime(9)
                    .AppRegisted = tmpTableReader.GetBoolean(10)
                    .AppRegCode = tmpTableReader.GetString(11)

                End With
            End If

        Catch ex As Exception

            mErrorNumber = -1001
            mErrorMessage = ex.Message
            mErrorCreater = "clsAppDbAccess - ConvertRecordToAppClass"

            Return Nothing
        End Try

        Return tmpCls

    End Function

End Class
